/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.schedule.executor.manager;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;

import static org.quartz.JobBuilder.*;

import com.hyts.schedule.executor.base.AbstractJobManager;
import com.hyts.schedule.executor.base.BaseSchedulerManager;
import com.hyts.schedule.executor.error.ScheduleError;
import com.hyts.schedule.executor.handler.Task;
import com.hyts.schedule.executor.model.JobBean;

/** 
 * @title SimpleJobManager.java  
 * @package com.hyts.schedule.quartz.manager  
 * @description 简单的工作任务管理器  
 * @see com.hyts.schedule.executor.base.AbstractJobManager<AbstractJob<JobBean>, Trigger>
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月18日 
 * @version V1.0 
 */
public final class DefaultJobManager implements AbstractJobManager<Task, Trigger> {

	/* (非 Javadoc)
	 * @param job
	 * @param trigger
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractJobManager#addJob(java.lang.Object, java.lang.Object)  
	 * @exception
	 */ 
	@Override
	public Date addJob(Task job, Trigger trigger) {
		JobDetail jobDetail = newJob(job.getClass()).
							  usingJobData(job.getJobBean().getJobName()+"-"+job.getJobBean().getJobGroupName(),job.getJobBean().
									  getParamJson().toJSONString()).
							  withIdentity(job.getJobBean().
									  getJobName(),job.getJobBean().getJobGroupName()).
							  withDescription(job.getJobBean().
									  getDescription()).
							  build();
		try {
			return BaseSchedulerManager.getInstance().scheduleJob(jobDetail,trigger);
		} catch (SchedulerException e) {
			throw new ScheduleError(e);
		}
	}

	/* (非 Javadoc)
	 * @param trigger
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractJobManager#addJob(java.lang.Object)  
	 * @exception
	 */ 
	@Override
	public Date addJob(Trigger trigger) {
		try {
			return BaseSchedulerManager.getInstance().scheduleJob(trigger);
		} catch (SchedulerException e) {
			throw new ScheduleError(e);
		}
	}

	/* (非 Javadoc)
	 * @param triggerAndJobs
	 * @see com.hyts.schedule.quartz.base.AbstractJobManager#addJobs(java.util.Map)  
	 * @exception
	 */ 
	@Override
	public void addJobs(
			Map<Task, List<Trigger>> triggerAndJobs) {
		
	}

}
